#ifndef UUID_F03E500776F248FAE013E7A60F3E2BFB
#define UUID_F03E500776F248FAE013E7A60F3E2BFB

#include <gvl/support/platform.h>


GVL_INLINE void bytewriter_init(bytewriter* s)
{
	s->next = s->end = 0;
	s->user = 0;
	s->overflow = bytewriter_overflow_dummy;
	s->flush = bytewriter_flush_dummy;
	s->destroy = 0;
	s->message_ends = 0;
}

GVL_INLINE void bytewriter_destroy(bytewriter* s)
{
	if(s->destroy)
		s->destroy(s);
}

GVL_INLINE void bytewriter_put(bytewriter* s, unsigned char b)
{
	if(s->next != s->end)
		*s->next++ = b;
	else
		s->overflow(s, b);
}

GVL_INLINE int bytewriter_left(bytewriter* s)
{
	return s->end - s->next;
}

GVL_INLINE void bytewriter_flush(bytewriter* s)
{
	s->flush(s);
	s->overflow(s, -1);
}

//

GVL_INLINE void bytereader_init(bytereader* s)
{
	s->next = s->end = 0;
	s->user = 0;
	s->next_unreceived_message = 0;
	s->next_complete_message_end = 0; // (next_unreceived_message == next_complete_message_end) forces underflow
	s->underflow = bytereader_underflow_dummy;
	s->update = bytereader_update_dummy;
	s->destroy = 0;
}

GVL_INLINE void bytereader_destroy(bytereader* s)
{
	if(s->destroy)
		s->destroy(s);
}

GVL_INLINE int bytereader_empty(bytereader* s)
{
	return s->next == s->end && !s->underflow(s);
}

GVL_INLINE int bytereader_left(bytereader* s)
{
	return s->end - s->next;
}

GVL_INLINE unsigned char bytereader_get(bytereader* s)
{ return (s->next != s->end ? *s->next++ : bytereader_fix_underflow_(s)); }

#endif // UUID_F03E500776F248FAE013E7A60F3E2BFB
